import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * Message API返回消息管理
 * @date: 2019-1-9


 */
import { getResponse, createPagination, parseParameters } from 'utils/utils';
import { queryIdpValue } from 'cpaas-front/lib/services/api';
import { deleteMessage as _deleteMessage, fetchMessageList as _fetchMessageList, createMessage as _createMessage, updateMessage as _updateMessage, getMessageDetail as _getMessageDetail } from "../services/messageService";
export default {
  namespace: 'message',
  state: {
    messageList: [],
    // 消息列表
    languageList: [],
    // 语言列表
    pagination: {},
    // 分页对象
    messageDetail: {},
    // 查询列表
    messageType: [] // 类型列表

  },
  effects: {
    // 获取初始化数据
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_, _ref) {
      var call, put, messageType;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.t0 = getResponse;
              _context.next = 4;
              return call(queryIdpValue, 'CPFM.MESSAGE_TYPE');

            case 4:
              _context.t1 = _context.sent;
              messageType = (0, _context.t0)(_context.t1);
              _context.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  messageType: messageType
                }
              });

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    // 获取消息列表
    fetchMessageList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchMessageList(_ref2, _ref3) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchMessageList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.next = 4;
              return call(_fetchMessageList, parseParameters(payload));

            case 4:
              res = _context2.sent;
              list = getResponse(res);

              if (!list) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  messageList: list.content,
                  pagination: createPagination(list)
                }
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchMessageList);
    }),
    // 查询消息列表
    getMessageDetail: /*#__PURE__*/_regeneratorRuntime.mark(function getMessageDetail(_ref4, _ref5) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function getMessageDetail$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call, put = _ref5.put;
              _context3.next = 4;
              return call(_getMessageDetail, payload);

            case 4:
              res = _context3.sent;
              list = getResponse(res);

              if (!list) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  messageDetail: list
                }
              });

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, getMessageDetail);
    }),
    // 新增消息
    createMessage: /*#__PURE__*/_regeneratorRuntime.mark(function createMessage(_ref6, _ref7) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createMessage$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(_createMessage, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, createMessage);
    }),
    // 更新消息
    updateMessage: /*#__PURE__*/_regeneratorRuntime.mark(function updateMessage(_ref8, _ref9) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateMessage$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call;
              _context5.next = 4;
              return call(_updateMessage, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, updateMessage);
    }),
    // 删除消息
    deleteMessage: /*#__PURE__*/_regeneratorRuntime.mark(function deleteMessage(_ref10, _ref11) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteMessage$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call;
              _context6.next = 4;
              return call(_deleteMessage, payload);

            case 4:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, deleteMessage);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref12) {
      var payload = _ref12.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};